import de.danielbechler.diff.node.Node;
import de.danielbechler.diff.visitor.PrintingVisitor;
import de.danielbechler.util.Strings;

public class Visitor extends PrintingVisitor {

	private StringBuilder sb = new StringBuilder(200);

	public Visitor(Object working, Object base) {
		super(working, base);
	}

	@Override
	protected String differenceToString(Node difference, Object base,
			Object modified) {
			return translateState(difference.getState(),
					difference.canonicalGet(base),
					difference.canonicalGet(modified),difference.getPropertyPath().toString().substring(1));
	}

	private String translateState(final Node.State state, final Object base,
			final Object modified, String path) {
		if (state == Node.State.IGNORED) {
			return path+" 被忽略";
		} else if (state == Node.State.CHANGED) {
			return String.format("%s 修改: [ %s ] ==> [ %s ]", path,
					Strings.toSingleLineString(base),
					Strings.toSingleLineString(modified));
		} else if (state == Node.State.ADDED) {
			return String.format("%s 增加: [ %s ]", path,
					Strings.toSingleLineString(modified));
		} else if (state == Node.State.REMOVED) {
			return String.format("%s 删除: [ %s ]", path,
					Strings.toSingleLineString(base));
		} else if (state == Node.State.UNTOUCHED) {
			return path + " 无变化";
		}
		return '(' + state.name() + ')';
	}
	
	public String getChange(){
		return sb.toString();
	}
	@Override
	protected void print(String text) {
		sb.append(text);
		sb.append('\n');
	}

}
